package ink.xiaobaibai.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import ink.xiaobaibai.bo.UserSelectBo;
import ink.xiaobaibai.entity.AdminUser;
import ink.xiaobaibai.vo.UserByAdminVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author xiaobaibai
 * @since 2021-05-11
 */
public interface AdminUserDao extends BaseMapper<AdminUser> {

    public int getUserVoByAdminCount(@Param("userSelectBo") UserSelectBo userSelectBo);

    public List<UserByAdminVo> getUserVoByAdminList(@Param("userSelectBo") UserSelectBo userSelectBo);

    @Select("SELECT count(*) FROM admin_user WHERE id=#{userId1} AND department_id=(SELECT department_id FROM admin_user WHERE id=#{userId2})")
    public Integer identicalDep(@Param("userId1") Integer userId1, @Param("userId2") Integer userId2);

    @Select("SELECT id FROM admin_user WHERE department_id = (SELECT department_id FROM admin_user WHERE id= #{userId})")
    public List<Integer> getDepUserIdByUserId(@Param("userId") Integer userId);

    @Select("SELECT r.role_name FROM admin_role r LEFT JOIN admin_user_role ur ON ur.role_id=r.id WHERE ur.user_id=#{userId}")
    public List<String> getRoleListByUserId(@Param("userId") Integer userId);

    @Select("SELECT u.`user_name`,u.`user_phone`,u.`department_id` FROM admin_user u LEFT JOIN admin_user_role ur ON ur.`user_id`=u.id WHERE ur.`role_id`=(SELECT id FROM admin_role WHERE role_name = 'agent')")
    public List<AdminUser> getAgentUser();

    @Select("SELECT COUNT(*) FROM admin_user_role WHERE user_id = #{userId} AND role_id = (SELECT id FROM admin_role WHERE role_name = '_supplier')")
    public boolean isSupplierAdminUser(@Param("userId") Integer userId);

    @Select("SELECT u.id,u.user_name,u.user_phone FROM admin_user u LEFT JOIN admin_user_role ur ON u.id = ur.user_id WHERE u.department_id = #{depId} AND ur.role_id = (SELECT arr.id FROM admin_role arr WHERE arr.role_name = 'agent') LIMIT 1")
    public AdminUser getAgentByDepId(@Param("depId") Integer depId);

}
